package com.demo.aidroid.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.demo.aidroid.R;
import com.demo.aidroid.bean.Book;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by kevinLiao on 2017/7/1.
 *
 * @author kevinLiao
 */
public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.DefaultViewHolder> {
    private final static int ITEM_DEFAULT = 0x1;
    private final static int ITEM_SELECTED = 0x2;

    private Context mContext;
    private List<Book> mList = new ArrayList<>();
    private LayoutInflater mInflater;
    private int selectedIndex = 0;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(RecyclerView.ViewHolder holder, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public BookListAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setItemSelectedEntity(int index) {
        selectedIndex = index;
        notifyItemChanged(selectedIndex);
    }

    @Override
    public DefaultViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_DEFAULT) {
            return new DefaultViewHolder(mInflater.inflate(R.layout.book_list_item_default, parent, false));
        } else {
            return new SelectedViewHolder(mInflater.inflate(R.layout.book_list_item_selected, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(DefaultViewHolder holder, int position) {
        Book book = getItem(position);

        holder.tvNumber.setText(getString(R.string.number, position + 1));
        holder.tvName.setText(book.getName());
        holder.tvShell.setText(book.getLocation());

        if (holder instanceof SelectedViewHolder) {
            SelectedViewHolder viewHolder = (SelectedViewHolder) holder;
            viewHolder.imgThumb.setImageResource(book.getImgRes());
            viewHolder.tvNumber.setTextColor(mContext.getResources().getColor(R.color.colorAccent));
            viewHolder.tvName.setTextColor(mContext.getResources().getColor(R.color.colorAccent));
        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public Book getItem(int pos) {
        return mList.get(pos);
    }

    @Override
    public int getItemViewType(int position) {
        return (position == selectedIndex) ? ITEM_SELECTED : ITEM_DEFAULT;
    }

    public void setList(List<Book> books) {
        mList.clear();
        mList.addAll(books);
        notifyDataSetChanged();
    }

    public class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @InjectView(R.id.tv_number)
        TextView tvNumber;
        @InjectView(R.id.tv_name)
        TextView tvName;
        @InjectView(R.id.tv_shell)
        TextView tvShell;

        DefaultViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(this, getAdapterPosition());
            }
        }
    }

    public class SelectedViewHolder extends DefaultViewHolder {
        @InjectView(R.id.img_thumb)
        ImageView imgThumb;

        SelectedViewHolder(View itemView) {
            super(itemView);
        }
    }

    private String getString(int id, Object... obj) {
        return mContext.getString(id, obj);
    }
}
